package com.ztk.suanfa;

import java.util.Arrays;

/**
 * 删除数组中的重复元素
 * 数组为有序递增数组
 */
public class _03_Shan_chu_chong_fu_yuan_su {

    public static void main(String[] args) {
        int[] nums = {1, 1, 2, 3, 3, 5, 5, 7};

        System.out.println(removeDuplicates(nums));
        Arrays.stream(nums).forEach(System.out::println);
    }

    public static int removeDuplicates(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }

        int slow = 1;
        int fast = 1;

        while (fast < n) {
            if (nums[fast] != nums[fast-1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }

        return slow--;
    }
}
